unit uFrmMainLPT;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ufrmtable, umainutils, Oracle, inifiles, pngimage, gnugettext,
  StdCtrls, ufrmclock, DB, OracleData, uImagePanel, ufrmwebbrowser;

type
  TFrmMainLPT = class(TForm)
    pnl_Bottom: TPanel;
    pnl_Drink: TPanel;
    Img_StateDrink: TImage;
    pnl_Info: TPanel;
    img_StateInfoPlayer: TImage;
    pnl_WWW: TPanel;
    img_WWW: TImage;
    pnl_BreakTable: TPanel;
    img_BreakTable: TImage;
    pnl_Inspector: TPanel;
    img_Inspector: TImage;
    OracleQuery1: TOracleQuery;
    pnl_TopTable: TPanel;
    lbl_TOPTournament: TLabel;
    lbl_TOPTableName: TLabel;
    tmr_ClockUpdate: TTimer;
    OracleDataSet1: TOracleDataSet;
    pnl_Clock: TPanel;
    lbl_Message: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Img_StateDrinkClick(Sender: TObject);
    procedure img_StateInfoPlayerClick(Sender: TObject);
    procedure img_WWWClick(Sender: TObject);
    procedure img_BreakTableClick(Sender: TObject);
    procedure img_InspectorClick(Sender: TObject);
    procedure tmr_ClockUpdateTimer(Sender: TObject);
    procedure tmr_ScrollMsgTimer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    HaveClock: boolean;
    FrmWebBrowser: TFrmWebBrowser;
    { Private declarations }
  public
    FrmTable: TFrmTable;
    FrmMainClock: TFrmMainClock;
    procedure DeselectMenuButtons();
    { Public declarations }
  end;

const
  BottomPanelUpColor: TColor = $0071A96F;
  BottomPanelDownColor: TColor = clyellow;

var
  FrmMainLPT: TFrmMainLPT;

implementation

uses uUtils;
{$R *.dfm}

procedure TFrmMainLPT.FormCreate(Sender: TObject);
begin
  gnugettext.TranslateComponent(self);
  uUtils.pct_Temp := TPicture.Create();
  FrmTable := TFrmTable.Create(self);
  FrmTable.Parent := self;
  FrmTable.Align := alClient;
  FrmTable.pgc_Table.ActivePage := FrmTable.tbs_Table;
  FrmTable.pnl_PlayerMenu.Visible := false;
  FrmTable.Show;
  // -----
  pnl_Clock.Parent := FrmTable.tbs_Table;
  pnl_Clock.BringToFront;
  pnl_Clock.ParentBackground := true;
  FrmMainClock := TFrmMainClock.Create(self);
  FrmMainClock.HideTournamentName;
  FrmMainClock.Parent := pnl_Clock;
  FrmMainClock.OracleQueryPrizes.Session := OraSessMainUtils;
  FrmMainClock.OracleQuery1.Session := OraSessMainUtils;
  FrmMainClock.Align := alClient;
  FrmMainClock.Show;
  FrmMainClock.scaleBy(FrmMainClock.ClientHeight, 350);
  FrmTable.CheckMessages();
  FrmTable.lbl_TableStatus.BringToFront;
end;

procedure TFrmMainLPT.FormDestroy(Sender: TObject);
begin
FreeAndNil(pct_Temp);
end;

procedure TFrmMainLPT.FormResize(Sender: TObject);
begin
  if not FrmTable.SeatPositioning then
  begin
    // FrmTable.GetData('2', false, OracleDataSetTournament);
  end;
end;

procedure TFrmMainLPT.img_BreakTableClick(Sender: TObject);
var
  QueryStr: string;
begin
  if not FrmTable.m_IsCashGame and (FrmTable.m_TournamentID <> '') then
  begin
    if pnl_BreakTable.Color = BottomPanelDownColor then
    begin
      if MessageDlg(_('Cancel Breaking table?'), mtConfirmation, [mbYes, mbNo], 0) = mrYes then
      begin
        umainutils.ClearBreakTableData();
        DeselectMenuButtons();
        FrmTable.RefreshTableView();
        FrmTable.SendEventToServer(EventTypeBreakTable, FrmTable.m_TournamentID, FrmTable.m_TableID);
        exit;
      end
      else
      begin
        exit;
      end;
    end;
    if MessageDlg(_('Confirm break table?'), mtConfirmation, [mbYes, mbNo], 0) = mrYes then
    begin
      DeselectMenuButtons();
      pnl_BreakTable.Color := BottomPanelDownColor;
      // check if any  break table present
      OracleQuery1.Clear;
      OracleQuery1.SQL.Text := ('SELECT * FROM TABLES WHERE IS_BREAK_TABLE=1');
      OracleQuery1.Execute;
      if OracleQuery1.Eof then
      begin
        // check if enough free positions available
        QueryStr :=
          ' SELECT FREE-(SELECT PLAYING FROM GET_PLAYERS_PLAY_TICK_BY_TABLE GPP JOIN TABLES TA ON GPP.TABLE_ID=TA.ID WHERE TOURNAMENT_ID=' + FrmTable.m_TournamentID + ' AND TABLE_ID=' +
          FrmTable.m_TableID + ') AS REST_FREE FROM (SELECT NVL(SUM(GPP.SEATS-PLAYING),0) AS FREE FROM GET_PLAYERS_PLAY_TICK_BY_TABLE GPP' +
          ' JOIN TABLES TA ON GPP.TABLE_ID=TA.ID WHERE (GPP.TICKETS<>0 AND GPP.PLAYING<>0) AND TOURNAMENT_ID=' + FrmTable.m_TournamentID + ' AND TABLE_ID<>' + FrmTable.m_TableID + ')';
        OracleQuery1.Clear;
        OracleQuery1.SQL.Text := (QueryStr);
        OracleQuery1.Execute;
        if OracleQuery1.Field(0) >= 0 then
        begin
          // ---------
          QueryStr := 'SELECT TT.TABLE_ID FROM TOURNAMENT_TABLES TT WHERE TT.TOURNAMENT_ID=' + FrmTable.m_TournamentID + ' AND TT.TABLE_ID IN' +
            ' (SELECT TT.TABLE_ID FROM TOURNAMENT_TABLES TT LEFT JOIN GET_PLAYERS_PLAY_TICK_BY_TABLE GPP ON TT.TABLE_ID=GPP.TABLE_ID AND TT.TOURNAMENT_ID=GPP.TOURNAMENT_ID WHERE (GPP.TICKETS<>0 AND GPP.PLAYING<>0) AND TT.TOURNAMENT_ID=' + FrmTable.m_TournamentID + ')';
          UpdateDataToDB('TABLES', 'IS_BREAK_TABLE=1', 'ID=' + FrmTable.m_TableID);
          UpdateDataToDB('TABLES', 'MUST_SET_DEALER=1', 'ID IN (' + QueryStr + ')');
          // btn_BreakTable.Visible := false;
          // btn_CancelBreakTable.Visible := True;
          FrmTable.SendEventToServer(EventTypeBreakTable, FrmTable.m_TournamentID, FrmTable.m_TableID);
          FrmTable.RefreshTableView;
        end
        else
        begin
          ShowMessage(_('Not enough free positions on other tables!'));
          pnl_BreakTable.Color := BottomPanelUpColor;
        end;
      end
      else
      begin
        ShowMessage(_('First finish running breaking table!'));
        pnl_BreakTable.Color := BottomPanelUpColor;
      end;
    end;
  end;
end;

procedure TFrmMainLPT.img_InspectorClick(Sender: TObject);
begin
  FrmTable.pnl_PlayerMenu.Visible := false;
  FrmTable.StateOrderDrink := false;
  FrmTable.StateInfoPlayer := false;
  if pnl_Inspector.Color <> BottomPanelDownColor then
  begin
    if MessageDlg(_('Confirm call Inspector?'), mtWarning, [mbYes, mbNo], 0) = mrYes then
    begin
      InsertDataIntoDB('INSPECTOR_CALLS', 'TABLE_ID,DATETIME,VERIFIED', FrmTable.m_TableID + ',(SELECT SYSDATE FROM DUAL),0');
      // --------------
      pnl_Inspector.Color := BottomPanelDownColor;
      pnl_Drink.Color := BottomPanelUpColor;
      pnl_Info.Color := BottomPanelUpColor;
      pnl_WWW.Color := BottomPanelUpColor;
      pnl_BreakTable.Color := BottomPanelUpColor;
      FrmTable.SendEventToServer(EventTypeCallIns, FrmTable.m_TournamentID, FrmTable.m_TableID);
    end;
  end
  else
  begin
    if MessageDlg(_('Confirm CANCEL call Inspector?'), mtWarning, [mbYes, mbNo], 0) = mrYes then
    begin
      pnl_Inspector.Color := BottomPanelUpColor;
      UpdateDataToDB('INSPECTOR_CALLS', 'VERIFIED=1', 'TABLE_ID=' + FrmTable.m_TableID + ' AND VERIFIED=0');
      FrmTable.SendEventToServer(EventTypeCallIns, FrmTable.m_TournamentID, FrmTable.m_TableID);
    end;
  end;
  FrmTable.CheckMessages();
end;

procedure TFrmMainLPT.Img_StateDrinkClick(Sender: TObject);
begin
  FrmTable.pnl_PlayerMenu.Visible := false;
  FrmTable.StateInfoPlayer := false;
  if not FrmTable.StateOrderDrink then
  begin
    FrmTable.StateOrderDrink := true;
    pnl_Drink.Color := BottomPanelDownColor;
    pnl_Info.Color := BottomPanelUpColor;
    pnl_WWW.Color := BottomPanelUpColor;
    pnl_BreakTable.Color := BottomPanelUpColor;
    pnl_Inspector.Color := BottomPanelUpColor;
  end
  else
  begin
    FrmTable.StateOrderDrink := false;
    pnl_Drink.Color := BottomPanelUpColor;
  end;
end;

procedure TFrmMainLPT.img_StateInfoPlayerClick(Sender: TObject);
begin
  FrmTable.pnl_PlayerMenu.Visible := false;
  FrmTable.StateOrderDrink := false;
  if not FrmTable.StateInfoPlayer then
  begin
    if FrmMainClock.Visible then
    begin
      HaveClock := true;
      FrmMainClock.Visible := false;
    end;
    FrmTable.StateInfoPlayer := true;
    pnl_Info.Color := BottomPanelDownColor;
    pnl_Drink.Color := BottomPanelUpColor;
    pnl_WWW.Color := BottomPanelUpColor;
    pnl_BreakTable.Color := BottomPanelUpColor;
    pnl_Inspector.Color := BottomPanelUpColor;
  end
  else
  begin
    FrmMainClock.Visible := HaveClock;
    FrmTable.StateInfoPlayer := false;
    FrmTable.pgc_Table.ActivePage := FrmTable.tbs_Table;
    pnl_Info.Color := BottomPanelUpColor;
  end;
end;

procedure TFrmMainLPT.img_WWWClick(Sender: TObject);
begin
  FrmTable.pnl_PlayerMenu.Visible := false;
  FrmTable.StateOrderDrink := false;
  FrmTable.StateInfoPlayer := false;
  if pnl_WWW.Color <> BottomPanelDownColor then
  begin
    pnl_WWW.Color := BottomPanelDownColor;
    pnl_Drink.Color := BottomPanelUpColor;
    pnl_Info.Color := BottomPanelUpColor;
    pnl_BreakTable.Color := BottomPanelUpColor;
    pnl_Inspector.Color := BottomPanelUpColor;
    // -----o
    if FrmWebBrowser = nil then
    begin
      FrmWebBrowser := TFrmWebBrowser.Create(self);
      FrmWebBrowser.Parent := self;
      FrmWebBrowser.Show;
    end;
  end
  else
  begin
    DeselectMenuButtons;
  end;
end;

procedure TFrmMainLPT.tmr_ClockUpdateTimer(Sender: TObject);
begin
  if tmr_ClockUpdate.Interval <> 5000 then
    tmr_ClockUpdate.Interval := 5000;
  if FrmTable.m_TournamentID = '' then
  begin
    tmr_ClockUpdate.Enabled := false;
  end
  else
  begin
    OracleDataSet1.Close;
    OracleDataSet1.SQL.Clear;
    OracleDataSet1.Variables.List.Clear;
    OracleDataSet1.SQL.Text := 'SELECT * FROM GETPRESENTTOURNAMENTROUNDDATA WHERE TOURNAMENT_ID=' + FrmTable.m_TournamentID + ' ORDER BY ROUND_START DESC';
    OracleDataSet1.Open;
    FrmMainClock.GetData(OracleDataSet1);
  end;
end;

procedure TFrmMainLPT.tmr_ScrollMsgTimer(Sender: TObject);
begin
  if lbl_Message.Left + Canvas.TextWidth(lbl_Message.Caption) < 0 then
  BEGIN
    lbl_Message.Left := self.Width;
  END
  else
  begin
    lbl_Message.Left := lbl_Message.Left - 2;
  end;
end;

procedure TFrmMainLPT.DeselectMenuButtons;
begin
  FrmTable.StateOrderDrink := false;
  FrmTable.StateInfoPlayer := false;
  pnl_Drink.Color := BottomPanelUpColor;
  pnl_Info.Color := BottomPanelUpColor;
  pnl_WWW.Color := BottomPanelUpColor;
  pnl_BreakTable.Color := BottomPanelUpColor;
  pnl_Inspector.Color := BottomPanelUpColor;
  if FrmWebBrowser <> nil then
  begin
    FrmWebBrowser.Close;
    FreeAndNil(FrmWebBrowser);
  end;
end;

end.
